#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Author: Peng Ming Yuan
Module Description: show_utils ...
File: show_utils.py
Date: 2021/1/16
"""
from pyshare.const.enums.item import ItemType, ShowDressStatus, ClothesStatus
from pyshare.const.enums.role import RoleHeadUnlockType
from pyshare.dal.gameconfig.query import getProfileList
from pyshare.dal.repository_impl import ItemRepository, RoleRepository
from pyshare.utils import push_utils


def get_small_head_id_by_unlock(unlock_type, unlock_id):
    """
    通过解锁数据获取头像id
    :param unlock_type: 解锁类型
    :param unlock_id: 解锁id
    :return:
    """
    head_id = 0
    cfg_list = getProfileList('TcItemHead', order_by="ItemId")
    for cfg in cfg_list:
        for con in cfg.UnlockCondition:
            if con[0] == unlock_type and con[1] == int(unlock_id):
                return cfg.ItemId
    return head_id


def get_background_id_by_unlock(unlock_type, unlock_id, count=1):
    """
    通过解锁数据获取背景框id
    :param unlock_type: 解锁类型
    :param unlock_id: 解锁id
    :param count: 解锁数量
    :return:
    """
    background_id = 0
    cfg_list = getProfileList('TcItemBackground', order_by="Id")
    for cfg in cfg_list:
        for con in cfg.ConditionType:
            if con[0] == unlock_type and con[1] == int(unlock_id) and con[2] == count:
                return cfg.Id
    return background_id


def delete_role_clothes(clothes_id, rid_list):
    """
    删除角色穿戴的时装信息
    :param clothes_id:
    :param rid_list:
    :return:
    """
    for rid in rid_list:
        role_data = RoleRepository.get_by_rid(rid)
        show_id_dict = dict()
        item_list = [{"ItemType": ItemType.Clothes, "ItemId": int(clothes_id), "Count": 1}]
        # 穿戴玩家拥有的时装
        c = ItemRepository.get_forever_by_rid_itemtype(rid=rid, item_type=ItemType.Clothes)
        role_data.head_id = c.item_id
        show_id_dict["HeadId"] = c.item_id
        small_head_id = get_small_head_id_by_unlock(RoleHeadUnlockType.Clothes, clothes_id)
        # 小头像重置为1（默认头像）
        if small_head_id:
            item_list.append({"ItemType": ItemType.Head, "ItemId": small_head_id, "Count": 1})
        if role_data.small_head_id == small_head_id:
            c_cfg_list = getProfileList('TcItemHead', IsFree=1, Sex__in=[role_data.sex, 2], order_by='ItemId')
            role_data.small_head_id = c_cfg_list[0].ItemId
            show_id_dict["SmallHeadId"] = role_data.small_head_id
        # 背景框重置为0（默认背景框）
        background_id = get_background_id_by_unlock(ItemType.Clothes, clothes_id)
        if background_id:
            item_list.append({"ItemType": ItemType.Background, "ItemId": background_id, "Count": 1})
        if role_data.background_id == background_id:
            role_data.background_id = 0
            show_id_dict["BackgroundId"] = role_data.background_id
        RoleRepository.save(role_data)
        push_show_change(item_list, rid, ClothesStatus.Lost)
        push_show(show_id_dict, rid)


def dress_clothes(rid_list, clothes_id):
    """
    穿戴时装及时装解锁外显
    :param rid_list:
    :param clothes_id:
    :return:
    """
    role_data_list = RoleRepository.filter_by_rid_list(rid_list)
    for role_data in role_data_list:
        # 穿戴时装
        role_data.head_id = clothes_id
        item_list = [{"ItemType": ItemType.Clothes, "ItemId": clothes_id, "Count": 1}]
        show_id_dict = {"HeadId": clothes_id}
        # 读取通过时装解锁的背景框 有则穿戴
        background_id = get_background_id_by_unlock(ItemType.Clothes, clothes_id, 1)
        if background_id:
            role_data.background_id = background_id
            item_list.append({"ItemType": ItemType.Background, "ItemId": role_data.background_id, "Count": 1})
            show_id_dict["BackgroundId"] = role_data.background_id
        # 读取通过时装解锁的头像 有则穿戴
        small_head_id = get_small_head_id_by_unlock(RoleHeadUnlockType.Clothes, clothes_id)
        if small_head_id:
            role_data.small_head_id = small_head_id
            item_list.append({"ItemType": ItemType.Head, "ItemId": role_data.small_head_id, "Count": 1})
            show_id_dict["SmallHeadId"] = role_data.small_head_id
        RoleRepository.save(role_data)
        # 推送玩家获得的新外显
        push_show_change(item_list, role_data.role_guid, ClothesStatus.Get)
        push_show(show_id_dict, role_data.role_guid)


def dress_bubble(rid_list, bubble_id):
    """
    穿戴气泡框
    :param rid_list:
    :param bubble_id:
    :return:
    """
    role_data_list = RoleRepository.filter_by_rid_list(rid_list)
    for role_data in role_data_list:
        # 穿戴气泡框
        role_data.bubble_id = bubble_id
        RoleRepository.save(role_data)
        item_list = [{"ItemType": ItemType.Bubble, "ItemId": bubble_id, "Count": 1}]
        show_id_dict = {"BubbleId": bubble_id}
        # 推送玩家获得的新外显
        push_show_change(item_list, role_data.role_guid, ClothesStatus.Get)
        push_show(show_id_dict, role_data.role_guid)


def push_show(show_id_dict, rid):
    """
    当前穿戴外显推送
    :param show_id_dict: 小头像id
    :param rid: 角色id
    :return:
    """
    # show_id_dict的key要和TbRole的字段名相同
    push_utils.call_push_data(
        push_id=push_utils.PushId.SHOW_ID_CHANGE,
        data={
            "ShowIdDict": show_id_dict,
        },
        send_rid_list=[rid]
    )


def push_show_change(item_list, rid, status_type):
    """
    外显变更推送
    :param item_list: 道具列表  [{"ItemType":1,"ItemId":1, "Count": 1},...]
    :param rid: 角色id
    :param status_type: 变更情况
    :return:
    """
    push_utils.call_push_data(
        push_id=push_utils.PushId.SHOW_CHANGE,
        data={
            "ItemList": item_list,
            "GetFlag": status_type
        },
        send_rid_list=[rid]
    )


def delete_role_show(item_type, item_id, rid_list):
    """
    删除角色外显信息 新增活动结算外显类型时需要新增脱下逻辑
    :param item_type:
    :param item_id:
    :param rid_list:
    :return:
    """
    # 删除外显道具
    ItemRepository.delete_all_old_show_owner(item_type, item_id, rid_list)
    # 脱下玩家穿戴的气泡框
    if item_type == ItemType.Bubble:
        RoleRepository.update_bubble_old_owner_lost_now_set_bubble(rid_list, item_id)
    # 脱下玩家穿戴的时装（时装有对应解锁的头像和背景 也需要脱下）
    if item_type == ItemType.Clothes:
        delete_role_clothes(item_id, rid_list)
    # 脱下玩家穿戴的头像框
    if item_type == ItemType.HeadFrame:
        RoleRepository.update_head_frame_old_owner_lost_now_set_default(rid_list, item_id)


def dress_role_show(item_type, item_id, rid_list):
    """
    穿戴外显并推送
    :param item_type:
    :param item_id:
    :param rid_list:
    :return:
    """
    if item_type == ItemType.Bubble:
        dress_bubble(rid_list, item_id)
    # 穿戴时装（时装有对应解锁的头像和背景 也需要穿戴）
    if item_type == ItemType.Clothes:
        dress_clothes(rid_list, item_id)
